package com.wanshi.controller;

import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.wanshi.entity.AdminUser;
import com.wanshi.feign.UserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: wb
 * @CreateTime: 2023-03-07  14:13
 * @Description:
 */
@RestController  // @controller 和 @ResponseBody
@RequestMapping("/role")
//@CrossOrigin  // 解决跨域
@DefaultProperties(defaultFallback = "defaultFallback")  // 默认所有的方法都是用次方法来返回
public class RoleController {

    @Autowired
    private  RestTemplate restTemplate;

    @Autowired
    private DiscoveryClient discoveryClient;

    // 和注入service 一样脚后跟和脚后跟
    @Autowired
    private UserFeign userFeign;
    // restfull 的风格
    @GetMapping("/del/{id}") // delete
    public  String delete(@PathVariable("id") String admin_id){
//        String back = restTemplate.getForObject("http://localhost:9091/admin/getUser/1", String.class);
        // 从服务中心中,找user-server 这个服务,提供这个服务,可以是多个项目
        List<ServiceInstance> serviceInstanceList = discoveryClient.getInstances("user-server");
        // 从集合中获取一个
        ServiceInstance serviceInstance = serviceInstanceList.get(0);
        String url = "http://" + serviceInstance.getHost() + ":" + serviceInstance.getPort()+ "/admin/getUser/" + admin_id;
        String back = restTemplate.getForObject(url, String.class);
        System.out.println("admin 被调用了");
        return "del--user-success"+admin_id;
    }

    @GetMapping("/get2/{id}") // delete
    public  String get2(@PathVariable("id") String admin_id){
//        user-server 服务名
        String url = "http://USER-SERVER/admin/getUser/" + admin_id;
        System.out.println(url);
        String back = restTemplate.getForObject(url, String.class);
        System.out.println("admin 被调用了");
        return "del--user-success--->"+back;
    }
    @GetMapping("/geth/{id}") // delete
    @HystrixCommand(fallbackMethod = "queryByIdFallback")  // 一旦当前的方法错误.请立刻调用
    public  String geth(@PathVariable("id") String admin_id){
        String url = "http://USER-SERVER/admin/getadmin/" + admin_id;
        AdminUser adminUser = restTemplate.getForObject(url, AdminUser.class);
        System.out.println("admin 被调用了");
        return "del--user-success--->"+adminUser.getAdmin_id();
    }
    @GetMapping("/geth2/{id}") // delete
//    不需要 在指定方法了,需要加上HystrixCommand,被Hystrix管理
    @HystrixCommand
    public  String geth2(@PathVariable("id") String admin_id){
        String url = "http://USER-SERVER/admin/getadmin/" + admin_id;
        AdminUser adminUser = restTemplate.getForObject(url, AdminUser.class);
        System.out.println("admin 被调用了");
        return "del--user-success--->"+adminUser.getAdmin_id();
    }

    @GetMapping("/sleep/{id}") // delete  // 默认超时的时间5秒
//   Hystrix  请求超过1秒,我就认为出错
    @HystrixCommand(fallbackMethod = "queryByIdFallback")  // 一旦当前的方法错误.请立刻调用
    public  String sleep(@PathVariable("id") String admin_id){
        String url = "http://USER-SERVER/admin/getadmin/" + admin_id;
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("admin 被调用了");
        return "del--user-success--->sleep";
    }


    @GetMapping("/feign/{id}")
    @HystrixCommand(fallbackMethod = "queryByIdFallback") // 一旦userFeign 访问不成功 就会返回queryByIdFallback 的记过
    public  String feign(@PathVariable("id") String admin_id){
        AdminUser getadmin = userFeign.getadmin(admin_id);
        return "feign--user-success--->"+getadmin.getAdmin_id();
    }

    @GetMapping("/feigngetUser/{id}")
    public  String feigngetUser(@PathVariable("id") String admin_id){
        String port = userFeign.getUser(admin_id);
        return "feign--getUser--->"+port;
    }

    @GetMapping("/openfeign/{id}")
    public  AdminUser openfeign(@PathVariable("id") String admin_id){
        AdminUser getadmin = userFeign.getadmin(admin_id);
        return getadmin;
    }


    // 参数的列表必须一样,返回值没有要求
    public String queryByIdFallback(@PathVariable("id") String admin_id){
        return "对不起，网络太拥挤了！";
    }

    // 参数的列表必须一样,返回值没有要求
    public String defaultFallback(){
        return "我说controller 所有方法默认的返回结果！";
    }
}
